package com.snj.visangedu;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;

import com.snj.visangedu.drm.VideoAct;
import com.snj.visangedu.homestudy.AskItem;
import com.snj.visangedu.lib.CustomHttpClient;
import com.snj.visangedu.lib.Global;
import com.snj.visangedu.lib.PopupType;
import com.snj.visangedu.lib.StudyviewFragment;

public class GpsFragment extends Fragment implements OnClickListener {
	private final String TAG = "GpsFragment";
	
	boolean isblue = false;
	boolean isrunning = true;
	
	ImageView img_postscript = null;
	ImageView img_register = null;
	ImageView img_ask = null;
	ImageView img_ask2 = null;
	
	ScrollView scrv_edumax = null;
	
	LinearLayout lily_gps = null;
	LinearLayout lily_register = null;
	

		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_gps,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		init_widget();
	}
	
	private void init_widget() {
		View view = getView();
		
		LinearLayout.LayoutParams param = null;
		
		lily_gps = (LinearLayout)view.findViewById(R.id.lily_gps);
		
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int screenWidth = metrics.widthPixels;
		  
		
		
		for(int i=0; i<4; i++)
		{
			ImageView edumax1 = new ImageView(getActivity());
			param = new LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);		
			
			
			BitmapFactory.Options options = new BitmapFactory.Options();
			
			options.inJustDecodeBounds = true;
			//BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + i, "drawable", getActivity().getPackageName()), options);					
			options.inJustDecodeBounds = false;
			options.inPurgeable = true;
			options.inDither = true;
			options.inPreferredConfig = Config.RGB_565;
			edumax1.setAdjustViewBounds(true);
			
			Bitmap img = null;
			
			try
			{
				img = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("gps_" + i, "drawable", getActivity().getPackageName()), options);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			
			edumax1.setImageBitmap(img);
			
			  
			switch(i)
			{
			case 0:
				edumax1.setPadding(0, 100, 0, 0);
				param.topMargin = Global.dpToPx(getActivity(), 20);
				edumax1.setLayoutParams(param);
				options.inSampleSize = 1;					
				break;
			case 1:
				param.topMargin = Global.dpToPx(getActivity(), 50);
				edumax1.setLayoutParams(param);
				edumax1.setBackgroundResource(R.color.white_color);
				options.inSampleSize = 1;
				break;
			case 2:
				param.topMargin = Global.dpToPx(getActivity(), 30);
				edumax1.setLayoutParams(param);
				edumax1.setBackgroundResource(R.color.white_color);
				options.inSampleSize = 1;
				break;
			case 3:
				param.topMargin = Global.dpToPx(getActivity(), 30);
				edumax1.setLayoutParams(param);
				edumax1.setBackgroundResource(R.color.white_color);
				options.inSampleSize = 1;
				break;
			
			}
			
				
			edumax1.setScaleType(ScaleType.FIT_CENTER);
			lily_gps.addView(edumax1);
						
		}
		
		
		img_ask2 = new ImageView(getActivity());
		img_ask2.setPadding(0, 0, 0, 0);
		param = new LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
		param.topMargin = Global.dpToPx(getActivity(), 30);
		img_ask2.setLayoutParams(param);
		
		img_ask2.setAdjustViewBounds(true);
		img_ask2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gps_btn_ask_off));
		img_ask2.setBackgroundResource(R.color.white_color);
		lily_gps.addView(img_ask2);
		img_ask2.setScaleType(ScaleType.FIT_CENTER);
		
		img_ask2.setOnClickListener(this);
		
		//blank();
	}
	
	
	@Override
	public void onResume() {
		
		blank();		
		super.onResume();		
		
	}
	
	private void blank()
	{
		isrunning = true;
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				while(isrunning)
				{
					try {
						Thread.sleep(500);
						publishProgress("0");
						Thread.sleep(500);
						publishProgress("1");

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				return null;
			}

			@Override
			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				super.onProgressUpdate(values);
				
				try
				{
					if(values[0].equals("0"))
					{
						img_ask2.setImageResource(R.drawable.gps_btn_ask_on);
					}
					else
					{
						img_ask2.setImageResource(R.drawable.gps_btn_ask_off);
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				Log.d(TAG, "PostExecute");
			}
			
			
			
		}.execute("");
	}

	@Override
	public void onClick(View v) {
		
		if(v.equals(img_ask2))
		{
			((HomeActivity)getActivity()).Show_Popup(PopupType.YourPhone);
			
			
		}
	}
	
	

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		isrunning = false;
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		
		while(lily_gps.getChildCount() > 0)
		{
			if(lily_gps.getChildAt(0) instanceof ImageView)
			{
				ImageView imgv = (ImageView)lily_gps.getChildAt(0);
				
				Drawable d = imgv.getDrawable();
				if(d instanceof BitmapDrawable)
				{
					Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
					bitmap.recycle();
					bitmap = null;
				}
				
				
				d.setCallback(null);
			}
			lily_gps.removeViewAt(0);
		}
		
		if(lily_register != null)
		{
			while(lily_register.getChildCount() > 0)
			{
				ImageView imgv = (ImageView)lily_register.getChildAt(0);
				
				Drawable d = imgv.getDrawable();
				if(d instanceof BitmapDrawable)
				{
					Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
					bitmap.recycle();
					bitmap = null;
				}
				
				lily_register.removeViewAt(0);
				d.setCallback(null);
			}
		}
		super.onDestroy();
	}
	
	
	
}
